﻿<%@ Page Title="使用者权限管理" Language="C#" AutoEventWireup="true" CodeBehind="UserGroup.aspx.cs"
    Inherits="Page_UserGroup" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>群组权限管理</title>
    <link rel="stylesheet" type="text/css" href="../../css/theme.css" />
    <link rel="stylesheet" type="text/css" href="../../css/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="../../css/icon.css" />
    <script src="../../js/jquery-1.8.0.min.js" type="text/javascript"></script>
    <script src="../../js/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../../js/Json.js" type="text/javascript"></script>
    <script src="../../js/Common.js" type="text/javascript"></script>
    <script src="../../js/form.js" type="text/javascript"></script>
</head>
<body>
    <div id="container" style="width: 100%; padding: 5px">
        <div id="toolbar" style="text-align: left; padding-bottom: 10px">
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="pageJs.search()">搜索</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" id="add" onclick="pageJs.add()">
                新增</a> <a href="javascript:void(0)" class="easyui-linkbutton" id="edit" onclick="pageJs.edit()">
                    编辑</a> <a href="javascript:void(0)" class="easyui-linkbutton" id="del" onclick="pageJs.del()">
                        删除</a>
            <div style="padding-top: 5px">
                <span class="searchTitle">使用者代码</span>
                <input style="width: 107px" id="sUSERID" /><span class="searchBlock"> <span class="searchTitle">
                    使用者姓名</span>
                    <input style="width: 107px" id="sUSERName" />
                </span>
            </div>
        </div>
        <div id="divTable" style="text-align: left;">
            <div id="divGrid" style="float: left; width: 400px">
                <table id="dg" title="使用者列表" style="height: 350px; width: 380px" pagination="true"
                    autorowheight="false" pagesize="20" rownumbers="true" fitcolumns="false" singleselect="true">
                    <thead>
                        <tr>
                            <th field="USER_ID" width="100">
                                使用者代码
                            </th>
                            <th field="USER_NAME" width="120">
                                使用者姓名
                            </th>
                            <th field="COMPANY_CODE" width="80">
                                公司别
                            </th>
                            <th field="SIGN_PATH" width="120" data-options="formatter:formatFile">
                                签名
                            </th>
                        </tr>
                    </thead>
                </table>
                <div id="dlg" style="width: 440px; height: 450px; padding: 10px 20px;" title="add"
                    data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true" buttons="#dlg-buttons">
                    <div class="ftitle">
                        使用者明细</div>
                    <div class="fitem">
                        <label>
                            使用者代码:</label>
                        <input name="USER_ID" id="USER_ID" class="easyui-validatebox" style="ime-mode: disabled;"
                            disabled="disabled" />
                    </div>
                    <div class="fitem">
                        <label>
                            使用者姓名:</label>
                        <input name="" id="USER_NAME" class="easyui-validatebox" />
                        <input id="USER_KEY" type="hidden" />
                    </div>
                    <div class="fitem">
                        <label>
                            公司别代码:</label>
                        <input name="COMPANY_CODE" id="COMPANY_CODE" />
                    </div>
                    <div class="fitem">
                        <label>
                            密码:</label>
                        <input name="PASSWORD" id="PASSWORD" type="password" class="easyui-validatebox" style="width: 148px" />
                    </div>
                    <div class="fitem">
                        <label>
                            E_Mail:</label>
                        <input name="USER_MAIL" id="USER_MAIL" />
                    </div>
                    <div class="fitem">
                        <label>
                            联系电话:</label>
                        <input name="TELEPHONE" id="TELEPHONE" style="ime-mode: disabled;" />
                    </div>
                    <div class="fitem">
                        <label>
                            部门描述:</label>
                        <input name="DEP_DESC" id="DEP_DESC" />
                    </div>
                    <form name="frmUpdate" id="frmUpdate" method="post" action="../../ashx/UpdateFile.ashx?type=photo">
                    <div class="fitem">
                        <label>
                            签名:</label>
                        <input id="SIGN_PATH" name="SIGN_PATH" style="ime-mode: disabled;" type="file" />
                    </div>
                    </form>
                    <div class="fitem">
                        <label>
                        </label>
                        <input name="ACTIVE" id="ACTIVE" style="border: 0" type="checkbox" /><label style="padding-left: 2px"
                            for="ACTIVE">启用</label>
                    </div>
                    <div class="fitem">
                        <label>
                        </label>
                        <input name="SELF_FLAG" id="SELF_FLAG" style="border: 0" type="checkbox" />
                        <label style="padding-left: 2px"
                            for="ACTIVE">只查自己标识</label>
                    </div>
                </div>
                <div id="dlg-buttons">
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="pageJs.doSave()">
                        保存</a> <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel"
                            onclick="javascript:$('#dlg').dialog('close')">取消</a>
                </div>
            </div>
            <div id="divImg" style="position: absolute; width: 100px; height: 80px; display: none;
                z-index: 9999">
                <img id="userImg" style='width: 100px; height: 80px;' />
            </div>
            <div id="divGroup" class="easyui-tabs" style="overflow: hidden">
                <div title="使用者权限群组" style="padding: 5px; position: relative; overflow-x: auto">
                    <table border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td>
                                <div style="float: left">
                                    <table id="dgGroupAll" title="可选权限群组" style="height: 350px; width: 240px" pagination="false"
                                        autorowheight="false" rownumbers="true" fitcolumns="false">
                                        <thead>
                                            <tr>
                                                <th field="GROUP_ID" width="80">
                                                    群组代码
                                                </th>
                                                <th field="GROUP_DESC" width="100">
                                                    群组说明
                                                </th>
                                            </tr>
                                        </thead>
                                    </table>
                                </div>
                            </td>
                            <td>
                                <div style="width: 40px; float: left; text-align: center; padding-top: 30px">
                                    <a id="addAll" class="btn btn_1" href="javascript:void(0)" onclick="pageJs.addAllGroup();">
                                        <span class="outer"><span class="inner"><b>»</b></span></span></a><br>
                                    <br>
                                    <a id="addOne" class="btn btn_1" href="javascript:void(0)" onclick="pageJs.addGroup();">
                                        <span class="outer"><span class="inner"><b>›</b></span></span></a>
                                </div>
                            </td>
                            <td>
                                <table id="dgGroup" title="已选权限群组" style="width: 240px" toolbar="#delUserGroup">
                                </table>
                            </td>
                        </tr>
                    </table>
                    <div id="delUserGroup" style="text-align: left;">
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" id="delgroup"
                            plain="true" onclick="pageJs.delUserGroup()">删除</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        function formatFile(value, row) {
            if (value != null) return $.format("<a  id='img{0}' style='cussor:point' href='javascript:void(0)' onmouseover='toggleImg(\"{0}\",true,this);'  onmouseout='toggleImg(\"{0}\",false,this);'   class='imge'>", row.SIGN_PATH) + value.substring(0, 10) + $.format("</a><div style='position:absolute; width:100px;height:80px;display:none; z-index:9999'><img style='width:100px;height:80px;' src ='../../Files/UserPhoto/{0}' /><div>", row.SIGN_PATH);
            else return "";
        }
        function toggleImg(url, visible, ele) {
            if (visible == true) {
                $("#userImg").attr("src", "../../Files/UserPhoto/" + url);
                $("#divImg").css({ left: $(ele).offset().left, top: $(ele).offset().top + 15 }).show();

            }
            else $("#divImg").hide();
        }
        var pageJs = {
            url: "",
            validate: function() {
                var USERid = $.trim($("#USER_ID").val());
                var USERname = $.trim($("#USER_NAME").val());
                if (USERid == "" || USERid.length > 20)
                    return "使用者代码为必须项，最长20字符！";
                if (USERname == "" || USERname.length > 50)
                    return "使用者姓名为必须项，最长50字符！";
                var pwd = $.trim($("#PASSWORD").val());
                if (pwd == "" || pwd.length > 30) return "密码为必须项，最长30字符！";
                if ($.trim($("#USER_MAIL").val()).length > 50) return "E_Mail最长50字符！";
                if ($.trim($("#COMPANY_CODE").val()).length > 15) return "公司别代码最长15字符！";
                if ($.trim($("#TELEPHONE").val()).length > 20) return "联络电话最长20字符！";
                if ($.trim($("#DEP_DESC").val()).length > 20) return "部门描述最长100字符！";
                return "";

            },
            valImg: function(id) {
                var fileName = $('#' + id).val().toLowerCase();
                if (!/.(gif|jpg|jpeg|png|gif|jpg|png)$/.test(fileName)) return false;
                else return true;
            },
            delUserGroup: function() {
                var dataSel = $("#dgGroup").datagrid("getSelections");
                if (dataSel.length > 0) {
                    var gKeys = [];
                    var userKey = $("#dg").datagrid("getSelected").USER_KEY;
                    $.each(dataSel, function(i, newRow) {
                        gKeys.push({ USER_KEY: userKey, GROUP_KEY: newRow.GROUP_KEY, USER_GROUP_KEY: newRow.USER_GROUP_KEY });
                    });
                    $.post('../../ashx/System/hUserGroup.ashx?action=del', { data: JSON.stringify(gKeys) }, function(result) {
                        if (result.Success) {
                            //save user group
                            BasicJs.showMessage("删除成功！", true);
                            //reload user group
                            $('#dgGroup').datagrid("reload");

                        }
                        else {
                            //error
                            BasicJs.showMessage(result.Message);
                        }
                        //close dialog

                    }, "json"
                 );



                }
                else { BasicJs.showMessage("未选中要删除的权限群组！"); }
            },
            loadCombo: false,
            doLoadCombo: function() {
                $('#ORGAN_KEY').combobox({
                    data: organs,
                    valueField: 'ORGAN_KEY',
                    textField: 'ORGAN_Text'
                });

            },
            doAddGroup: function(dataSel) {
                if (dataSel && dataSel.length > 0) {
                    var dataExist = $("#dgGroup").datagrid("getRows");
                    //存放要添加的群组KEY
                    var gKeys = [];
                    var findItem;
                    $.each(dataSel, function(i, newRow) {
                        //先判断原先是否有数据
                        if (dataExist && dataExist.length > 0) {
                            findItem = $.grep(dataExist, function(itemExist) { return itemExist.GROUP_KEY == newRow.GROUP_KEY; });
                            //not exist
                            if (findItem.length == 0) {
                                gKeys.push(newRow.GROUP_KEY);
                            }
                        }
                        else {
                            gKeys.push(newRow.GROUP_KEY);
                        }
                    });

                    if (gKeys.length > 0) {
                        var parm = { USER_KEY: $("#dg").datagrid("getSelected").USER_KEY, GroupKeys: gKeys };
                        $.post('../../ashx/System/hUserGroup.ashx?action=add', { data: JSON.stringify(parm) }, function(result) {
                            if (result.Success) {
                                //save user group
                                BasicJs.showMessage("新增成功！", true);
                                //reload user group
                                $('#dgGroup').datagrid("reload");

                            }
                            else {
                                //error
                                BasicJs.showMessage(result.Message);
                            }
                            //close dialog

                        }, "json"
                     );

                    }


                }


            },
            addGroup: function() {
                var dataSel = $("#dgGroupAll").datagrid("getSelections");
                if (dataSel.length > 0) this.doAddGroup(dataSel);
                else BasicJs.showMessage("未选中要添加的权限群组！");
            },

            addAllGroup: function() {
                var dataSel = $("#dgGroupAll").datagrid("getRows");
                this.doAddGroup(dataSel);
            },


            doSave: function() {
                var valMsg = this.validate();
                if (valMsg != "") {
                    BasicJs.showMessage(valMsg);
                    return;
                }
                var active = 'N';
                if ($("#ACTIVE").attr("checked")) active = 'Y';
                var selfFlag = "N";
                if ($("#SELF_FLAG").attr("checked")) selfFlag = "Y";
                var parm = {
                    USER_KEY: $("#USER_KEY").val(), USER_ID: $.trim($("#USER_ID").val()), USER_NAME: $.trim($("#USER_NAME").val()),
                    COMPANY_CODE: $.trim($("#COMPANY_CODE").val()), DEP_DESC: $("#DEP_DESC").val(), PASSWORD: $("#PASSWORD").val(),
                    SIGN_PATH: "", USER_MAIL: $("#USER_MAIL").val(), TELEPHONE: $("#TELEPHONE").val(),
                    ACTIVE: active, SELF_FLAG: selfFlag
                };
                var action = "adduser";
                if ($("#USER_KEY").val() != "0") action = "edituser";
                var saveUser = function() {
                    $.post('../../ashx/System/hUserGroup.ashx?action=' + action, { data: JSON.stringify(parm) }, function(result) {
                        if (result.Success) {
                            if ($("#USER_KEY").val() != "0") {
                                //edit
                                var callback = function() { $('#dlg').dialog('close'); };
                                BasicJs.showMessage("保存成功！", true, callback);

                            }
                            else {//add
                                BasicJs.showMessage("新增成功！", true);
                                pageJs.initAdd();
                                setTimeout('$("#USER_NAME").focus()', 100);
                            }
                            $('#dg').datagrid('reload');

                        }
                        else {
                            //error
                            BasicJs.showMessage(result.Message);
                        }
                        //close dialog

                    }, "json"
             );
                };
                //先上传签名
                if ($("#SIGN_PATH").val() != '') {
                    if (pageJs.valImg("SIGN_PATH")) {
                        $("#frmUpdate").ajaxSubmit({
                            data: { fname: parm.COMPANY_CODE + parm.USER_ID, objName: "user" },
                            success: function(data) {
                                if (data.Success) {
                                    $("#SIGN_PATH").clearInputs();
                                    //更新文件名
                                    parm.SIGN_PATH = data.Message;
                                    saveUser();
                                }
                                else {
                                    if (data.Message) BasicJs.showMessage(result.Message);
                                }
                            }, // post-submit callback 
                            dataType: "json"
                        });
                    }
                    else {
                        BasicJs.showMessage("签名必须是.gif,jpeg,jpg,png中的一种", 0);
                        return;
                    }
                }
                else {
                    if (action == "edituser") {
                        var row = $('#dg').datagrid('getSelected');
                        if (row.SIGN_PATH) parm.SIGN_PATH = row.SIGN_PATH;
                    }
                    saveUser();
                }

            }

,

            initAdd: function() {
                $("#USER_KEY").val("0");
                $("#USER_ID").val("").removeAttr("disabled");
                $("#COMPANY_CODE,#DEP_DESC,#PASSWORD,#USER_NAME,#USER_MAIL,#TELEPHONE,#SIGN_PATH").val("");
                $("#ACTIVE").removeAttr("checked");
                $("#SELF_FLAG").removeAttr("checked");
                //             $('#ORGAN_KEY').combobox('setValue', "");
            },
            add: function() {
                // if (!this.loadCombo) { this.doLoadCombo(); }
                this.initAdd();
                var pos = BasicJs.getCenter("#dlg");
                var position = BasicJs.autoDialogSize(450, 440);
                $('#dlg').dialog(pos).dialog({
                    left: position.Left,
                    top: position.Top,
                    height: position.Height,
                    width: position.Width,
                    "onOpen": function() {
                        setTimeout(' $("#USER_ID").focus();', 100);
                    }
                }).dialog('open').dialog('setTitle', '新增使用者');

            },

            edit: function() {

                var row = $('#dg').datagrid('getSelected');
                if (row) {
                    // if (!this.loadCombo) { this.doLoadCombo(); }
                    $("#SIGN_PATH").val("");
                    $("#USER_KEY").val(row.USER_KEY);
                    $("#USER_ID").val(row.USER_ID).attr("disabled", "disabled");
                    $("#USER_NAME").val(row.USER_NAME);
                    $("#PASSWORD").val(row.PASSWORD);
                    $("#USER_MAIL").val(row.USER_MAIL);
                    $("#COMPANY_CODE").val(row.COMPANY_CODE);
                    $("#TELEPHONE").val(row.TELEPHONE);
                    $("#DEP_DESC").val(row.DEP_DESC);
                    if (row.ACTIVE == "Y") $("#ACTIVE").attr("checked", "checked");
                    else $("#ACTIVE").removeAttr("checked");

                    var pos = BasicJs.getCenter("#dlg");
                    var position = BasicJs.autoDialogSize(450, 440);
                    $('#dlg').dialog(pos).dialog({
                        left: position.Left,
                        top: position.Top,
                        height: position.Height,
                        width: position.Width,
                        "onOpen": function() {
                            setTimeout(' $("#USER_NAME").focus();', 100);
                        }
                    }).dialog('open').dialog('setTitle', '编辑使用者');
                }
                else { BasicJs.showMessage("未选中记录！"); }

            },

            del: function() {
                var row = $('#dg').datagrid('getSelected');
                if (row) {

                    $.messager.confirm('删除使用者', '您确定要刪除选中的使用者吗?', function(r) {
                        if (r) {
                            $.post(pageJs.url + "?action=deluser", { id: row.USER_KEY }, function(result) {
                                if (result.Success) {
                                    $('#dg').datagrid('reload');    // reload the user data
                                    BasicJs.showMessage("刪除成功！", true);
                                } else {
                                    if (result.Message) BasicJs.showMessage(result.Message);
                                    //                                 $.messager.show({   // show error message  
                                    //                                     title: 'Error',
                                    //                                     msg: result.errorMsg
                                    //                                 });
                                }
                            }, 'json');
                        }
                    });
                }
                else { BasicJs.showMessage("未选中记录！"); }


            },
            search: function() {
                $('#dg').datagrid('load',
                 {
                     isAdmin: false,
                     sUSERID: $.trim($("#sUSERID").val()),
                     sUSERName: $.trim($('#sUSERName').val())
                 }
            );
                $('#dgGroupAll').datagrid("reload");

            },

            dataFilter: function(data) {
                return data;
            },
            resize: function(h, w, init) {
                $('#container').height(h).width(w - 10);
                var contentH = h - $("#toolbar").outerHeight() - 10;
                if (contentH < 100) contentH = 100;
                var contentW = $("#divTable").width() - 420;
                if (contentW < 150) contentW = 150;
                $("#divGroup").width(contentW).height(contentH);
                $(".tabs-panels").width(contentW - 2).height(contentH - $(".tabs-header").outerHeight() - 2);
                $(".tabs-panels  .panel-body").width($(".tabs-panels").width() - 10).height($(".tabs-panels").height() - 10).css("overflow-x", "auto");

                $("#dg").datagrid("resize", { height: contentH });
                var gHeight = contentH - $(".tabs-header").outerHeight() - 14;
                $("#dgGroupAll").datagrid("resize", { height: gHeight });
                if (init) { }
                else $("#dgGroup").datagrid("resize", { height: gHeight });

            }

        }

        $(function () {
            //授权
            var funKey = window.name.substring(3);
            BasicJs.authorize("#add", funKey, "ADD");
            BasicJs.authorize("#edit", funKey, "EDIT");
            BasicJs.authorize("#del", funKey, "DELETE");
            BasicJs.authorize("#delgroup,#addOne,#addAll", funKey, "SETGROUP");

            $('#dg').datagrid(
                 {
                     url: "../../ashx/System/hUserGroup.ashx?action=getuser",
                     loadMsg: "正在加载数据，请稍后...",
                     onLoadSuccess: function () {
                         $('#dg').datagrid("selectRow", 0);

                     },
                     queryParams: { isAdmin: false },
                     onSelect: function (rowIndex, rowData) {
                         if (rowData) {
                             //使用者对应的权限群组
                             $('#dgGroup').datagrid({
                                 url: "../../ashx/System/hUserGroup.ashx?action=get&userkey=" + rowData.USER_KEY,
                                 loadMsg: "正在加载数据，请稍后...",
                                 columns: [[
                                 { field: 'GROUP_ID', title: '群组代码', width: 80 },
                                 { field: 'GROUP_DESC', title: '群组说明', width: 100 }
                                ]]
                             });
                             var contentH = $(window).height() - $("#toolbar").outerHeight() - 10;
                             if (contentH < 100) contentH = 100;
                             var gHeight = contentH - $(".tabs-header").outerHeight() - 12;
                             $("#dgGroup").datagrid("resize", { height: gHeight });

                         }
                     }

                 }
            );

            var pager = $('#dg').datagrid('getPager');
            pager.pagination({
                displayMsg: "共{total}条",
                beforePageText: "第",
                afterPageText: "页/共{pages}页"
            });
            //所有权限群组
            $('#dgGroupAll').datagrid(
                 {
                     url: "../../ashx/System/hGroup.ashx?action=get",
                     loadMsg: "正在加载数据，请稍后..."

                 }
            );
            pageJs.resize($(window).height(), $(window).width(), true);
            pageJs.url = '../../ashx/System/hUserGroup.ashx';


        });  
   
    
    </script>
</body>
<style type="text/css">   
                 .btn{border:0;display:inline-block;cursor:pointer;background:none;text-decoration:none; vertical-align:middle;}
.btn span{display:inline-block;height:100%;_overflow-y:hidden;*zoom:1}
.btn span:hover,.btn:hover{text-decoration:none;}
.btn .inner{white-space:nowrap;}
.btn i{margin-right:2px;vertical-align:middle;}
.btn .icoAdd{background-position:-64px -1px;_background-position:-64px 4px ; height:14px\9; _height:26px; _display:block; _float:left;}
.btn,.btn span,.btn:hover span,.btn:active span{background-image:url(../../images/btn_sprite.png);background-repeat:no-repeat;height:24px;line-height:24px;}
.btn{padding-left:5px;}
.btn .outer,.btn:hover .outer{padding-right:5px;}
.btn .inner,.btn:hover .inner{background-repeat:repeat-x;padding:0 5px;}
.btn_1{background-position:left top;}
.btn_1 .outer{background-position:right -54px; }
.btn_1 .inner{background-position:left -27px; vertical-align:top\9;}
.btn_1:hover{background-position:left -81px;}
.btn_1:hover .outer{background-position:right -135px;}
.btn_1:hover .inner{background-position:left -108px;}
.btn_2 {background-position:left -163px;}
.btn_2 .outer{background-position:right -217px;}
.btn_2 .inner{background-position:left -190px;color:#555555; vertical-align:top\9;}
         .ftitle{  
            font-size:14px;  
           font-weight:bold;  
           padding:5px 0;  
             margin-bottom:10px;  
             border-bottom:1px solid #ccc;  
        }  
        .fitem{  
           margin-bottom:5px;  
        }  
        .fitem label{  
             display:inline-block;  
            width:80px;  
         } 
          .searchBlock 
          {
              padding-left:20px;
              
              }
                .searchBlock span
          {
              
              
              }

          .searchTitle
         {
              padding-right:2px;
             }
     </style>
</html>
